﻿package Leophy.controls.Lists 
{
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import Leophy.controls.MotionClip.EasyMotionMovieClip;
	import Leophy.controls.MotionClip.MontionFuns;
	import Leophy.Interfaces.IControls.List.IListItem;
	
	/**
	 * ...
	 * @author TOPHY
	 */
	dynamic public class ListItem extends EasyMotionMovieClip implements IListItem
	{
		private var _selected:Boolean = false;
		private var _selecteAble:Boolean = false;
		protected var list:List;
		public function ListItem() 
		{
			this.addEventListener(MouseEvent.ROLL_OVER,RollOverFun);
			this.addEventListener(MouseEvent.ROLL_OUT, RollOutFun);
			this.addEventListener(MouseEvent.CLICK, clickfun);
			RollOutFun()
		}
		
		private function clickfun(ev:MouseEvent) {
			if (ev.currentTarget == this) {
				if (this.parent != null) {
					this.parent.swapChildren(this, this.parent.getChildAt(this.parent.numChildren - 1));
				}
				var ms:Boolean = false;
				if (list != null) {
					ms = list.MultSelect;
				}
				if(ms){
					this.setSelection(!getSelection());
				}else {
					this.setSelection(true);
				}
			}
		}
		/**
		 * 鼠标划过节点
		 * @param	ev
		 */
		protected function RollOverFun(ev:MouseEvent = null) {
			
			if (!_selected) {
				this.TimeMotionTo( { R: -30, G: -30, B: -30 }, MontionFuns.easeOutQuad, 200);
			}else{
				this.TimeMotionTo( { R:20, G:20, B:20 }, MontionFuns.easeOutQuad, 200);
			}
		}
		/**
		 * 鼠标划出节点
		 * @param	ev
		 */
		protected function RollOutFun(ev:MouseEvent = null) {
			if (!_selected) {
				this.TimeMotionTo( { R:-80, G:-80, B:-80 }, MontionFuns.easeOutQuad, 200);
			}else {
				this.TimeMotionTo( { R:20, G:20, B:20 }, MontionFuns.easeOutQuad, 200);
			}
		}
		/* INTERFACE Leophy.Interfaces.IControls.List.IListItem */
		public function get SelectedAble():Boolean {
			return this._selecteAble;
		}
		public function set SelectedAble(value:Boolean):void {
			if (this._selecteAble != value) {
				if (this._selected && !value) {
					this.setSelection(false);
				}
				this._selecteAble = value;
			}
		}
		/**
		 * 宽度
		 * @return
		 */
		public function itemWidth():Number
		{
			return this.width;
		}
		/**
		 * 高度
		 * @return
		 */
		public function itemHeight():Number
		{
			return this.height;
		}
		/**
		 * 是否支持鼠标行为
		 * @param	enable
		 */
		public function mouseEnable(enable:Boolean):void
		{
			this.mouseEnabled = enable;
		}
		/**
		 * 选中状态
		 * @return
		 */
		public function getSelection():Boolean {
			return this._selected;
		}
		/**
		 * 设置选中状态
		 * @param	selectedf
		 */
		public function setSelection(selectedf:Boolean):void
		{
			if (selectedf && !_selecteAble) return;
			
			if(this._selected!=selectedf){
				this._selected = selectedf;
				setListSelecteList();
			}
			RollOverFun();
		}
		private function setListSelecteList() {
			if(list!=null){
				if (this._selected) {
					list.AddSelectedItem(this);
				}else {
					list.DelSelectedItem(this);
				}
			}
		}
		/**
		 * x位置
		 * @return
		 */
		public function itemX():Number
		{
			return this.x;
		}
		/**
		 * y位置
		 * @return
		 */
		public function itemY():Number
		{
			return this.y;
		}
		
		public function setItemX(value:Number):void
		{
			this.x = value;
		}
		
		public function setItemY(value:Number):void
		{
			this.y = value;
		}
		
		public function setVisible(f:Boolean):void
		{
			this.visible = f;
		}
		
		public function getVisible():Boolean
		{
			return this.visible;
		}
		
		override public function toString():String 
		{
			return "ITEM:"+this.name;
		}
		
		/**
		 * [内部调用]绑定list
		 * @param	list
		 */
		public function bindList(list:List):void
		{
			this.list = list;
			setListSelecteList();
		}
		
		
	}
	
}